if(util==null) var util = {};

if (document.getElementById) {
  util.byId = function() {
    var elements = new Array();
    for (var i = 0; i < arguments.length; i++) {
      var element = arguments[i];
      if (typeof element == 'string') {
        element = document.getElementById(element);
      }
      if (arguments.length == 1) {
        return element;
      }
      elements.push(element);
    }
    return elements;
  };
}
else if (document.all) {
  util.byId = function() {
    var elements = new Array();
    for (var i = 0; i < arguments.length; i++) {
      var element = arguments[i];
      if (typeof element == 'string') {
        element = document.all[element];
      }
      if (arguments.length == 1) {
        return element;
      }
      elements.push(element);
    }
    return elements;
  };
}

//var $;
//if (!$) {
//  $ = util.byId;
//}

util.removeAllRows = function(ele, options) {
  ele = util._getElementById(ele, "removeAllRows()");
  if (ele == null) return;
  if (!options) options = {};
  if (!options.filter) options.filter = function() { return true; };
  if (!util._isHTMLElement(ele, ["table", "tbody", "thead", "tfoot"])) {
    return;
  }
  var child = ele.firstChild;
  var next;
  while (child != null) {
    next = child.nextSibling;
    if (options.filter(child)) {
      ele.removeChild(child);
    }
    child = next;
  }
}

util._isHTMLElement = function(ele, nodeName) {
  if (ele == null || typeof ele != "object" || ele.nodeName == null) {
    return false;
  }
  if (nodeName != null) {
    var test = ele.nodeName.toLowerCase();
    if (typeof nodeName == "string") {
      return test == nodeName.toLowerCase();
    }
    if (util._isArray(nodeName)) {
      var match = false;
      for (var i = 0; i < nodeName.length && !match; i++) {
        if (test == nodeName[i].toLowerCase()) {
          match =  true;
        }
      }
      return match;
    }
    return false;
  }
  return true;
}

util.cloneNode = function(ele, options) {
  ele = util._getElementById(ele, "cloneNode()");
  if (ele == null) return null;
  if (options == null) options = {};
  var clone = ele.cloneNode(true);
  if (options.idPrefix || options.idSuffix) {
    util._updateIds(clone, options);
  }
  else {
    util._removeIds(clone);
  }
  ele.parentNode.insertBefore(clone, ele);
  return clone;
}

util._updateIds = function(ele, options) {
  if (options == null) options = {};
  if (ele.id) {
    ele.setAttribute("id", (options.idPrefix || "") + ele.id + (options.idSuffix || ""));
  }
  var children = ele.childNodes;
  for (var i = 0; i < children.length; i++) {
    var child = children.item(i);
    if (child.nodeType == 1 /*Node.ELEMENT_NODE*/) {
      util._updateIds(child, options);
    }
  }
}

util._removeIds = function(ele) {
  if (ele.id) ele.removeAttribute("id");
  var children = ele.childNodes;
  for (var i = 0; i < children.length; i++) {
    var child = children.item(i);
    if (child.nodeType == 1 /*Node.ELEMENT_NODE*/) {
      util._removeIds(child);
    }
  }
}

util._getElementById = function(ele, source) {
  var orig = ele;
  ele = util.byId(ele);
  if (ele == null) {
    return ele;
  }
  return ele;
}

util._isArray = function(data) {
  return (data && data instanceof Array);
}

util.convertDateVN = function (vdate){
    if(vdate==null||vdate=='') return '';
    var day = vdate.getDate().toString();
    if(day.length<2) day = "0"+day;
    var month = vdate.getMonth()+1;
    if (month.toString().length<2) month = "0"+month;
    var year = vdate.getFullYear();
    day = day+"/"+month+"/"+year;
    return (day);
}
util.convertDateOther = function (vdate){
    if(vdate==null||vdate=='') return '';
    alert(vdate);
    var day = vdate.getDate();
    if(day.length<2) day = "0"+day;
    var month = vdate.getMonth()+1;
    if (month.length<2) month = "0"+month;
    var year = vdate.getFullYear();
    day = month+"/"+day+"/"+year;
    return (day);
}

util.getChecked = function (parentId,type, exceptionId){
    var result = '';
    var grid = document.getElementById(parentId);
    var allc = grid.getElementsByTagName("input");
    for (i=0;i<allc.length;i++){
        if (allc[i].type==type&&allc[i].checked&&allc[i].id!=exceptionId){
            result += (result=='')?(allc[i].value):(','+allc[i].value);	
        }
    }
    return result;
}

<!-- ALL TRIM FUNCTION-->
/*
 PROTOTYPE  : Trim(s)
 INPUT PARAM: STRING
 PROCESS    : Cat bo cac ky tu white space o 2 dau` chuoi
 OUTPUT     : CHUOI SAU KHI CAT
 */
util.Trim = function (s){
    if(s!=null&&s.length>0){
        while ((s.substring(0,1) == ' ') || (s.substring(0,1) == '\n') || (s.substring(0,1) == '\r')){
            s = s.substring(1,s.length);
        }
        while ((s.substring(s.length-1,s.length) == ' ') || (s.substring(s.length-1,s.length) == '\n') || (s.substring(s.length-1,s.length) == '\r')){
            s = s.substring(0,s.length-1);
        }
    }
    return s;
}

util.isDate = function (strDate) {
    var re = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{2,4})$/;
    if (!re.test(strDate)) return false;
    strDate = strDate.split('/');
    if(strDate.length==3){
        year = parseInt(strDate[2]);
        while(strDate[1].substr(0,1)=='0' && strDate[1].length>1) strDate[1] = strDate[1].substr(1);
        month = parseInt(strDate[1]);
        while(strDate[0].substr(0,1)=='0' && strDate[0].length>1) strDate[0] = strDate[0].substr(1);
        day = parseInt(strDate[0]);
        // month argument must be in the range 1 - 12
        month = month - 1; // javascript month range : 0- 11
        var tempDate = new Date(year,month,day);
        if ( (tempDate.getFullYear() == year) &&
            (month == tempDate.getMonth()) &&
            (day == tempDate.getDate()) )
            return true;
    }
    return false;
}
util.closePanel = function (elmName) {
    if ($(elmName) != null){
        $(elmName).style.display = "none";
    } 
}

util.isImage = function (fileName){	
	var dotpos;
	fileName = fileName.substring(fileName.lastIndexOf("\\")+1,fileName.length);
	dotpos = fileName.lastIndexOf('.');
	ext = fileName.substr(dotpos+1,fileName.length-dotpos);
	ext = ext.toLowerCase();	
	if (ext != ""){			
		if ((ext != "jpg") && (ext != "gif") && (ext != "bmp") && (ext != "swf")){
			return false;
		}else{
			return true;
		}
	}else{
		return true;
	}
}
/* dvchinh */

var ascii = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.";
var spec="~`!@#$%^*+=|\\{}[];:'\"<>/"; //ky tu dac biet   
var specFull="~`!@#$%^*+_-?.,=|\\{}[];:'\"<>/"; //ky tu dac biet   
var specNNKD="~`!@#$%^*+=|\\{}[]:'\"<>/"; //ky tu dac biet 
var specCharLittle="~`!@#$%^*+=|{};:'\"<>?"; //ky tu dac biet   
var date = "/" + number;
var number = "0123456789";
var curency = number + ".,";
var passchar = ascii + spec + "&-_";
var phone = " -().eEx" + number;
var pattern = ".";
var nonemail = "~`!#$%^*()+=|\\{}[];:'\"<>,/?";
var nonaddress = "~`!@#$%^*()+=|\\{}[];:'\"<>?";
var nonwebsite = "~`!@#$%^*()+=|\\{}[];'\"<>?";
var nonchar = spec + "&-_" + number; 
var variable = ascii + "&-_";

/*
    @author: dvchinh
    Ham kiem tra gia tri nhap lieu hop le
    Tra ve xau loi neu chua hop le, tra ve xau rong neu du lieu hop le
    Input:
        - Name: ten doi tuong
        - greater: do dai toi da cua doi tuong
        - lower: do dai toi thieu
        - argSpecChar: chuoi du lieu so sanh
        - except: 0: chi nhap cac ky tu trong chuoi argSpecChar, 1: khong nhap cac ky tu trong chuoi argSpecChar
        - isUpCase: 0: khong chuyen doi tuong thanh chu hoa, 1: chuyen doi tuong thanh chu hoa
        - messpart: cau thong bao khi co loi
    Output:Tra ve xau loi neu chua hop le, tra ve xau rong neu du lieu hop le
*/
function isValid(Name,greater,lower,argSpecChar,except,isUpCase,messpart){
    var strMsg = "";
    var ctrName = document.getElementById(Name).value;
    try{
    ctrName = util.Trim(ctrName);
    }catch (e) {};
    if (isUpCase==1){
        var str = ctrName.toUpperCase();
        ctrName = str;
        document.getElementById(Name).innerText = str;
    }
    if (lower==-1){
	    if (ctrName.length>0)
		    if (ctrName.length>greater)
			    strMsg += "\n - " + messpart + " ph\u1EA3i nh\u1ECF h\u01A1n " + (greater+1) + " k\u00FD t\u1EF1!";
    }else{
        if (ctrName.length==0){
		    strMsg += "\n - Xin nh\u1EADp v\u00E0o " + messpart + "!";
        }else if (ctrName.length>greater || ctrName.length<lower){
            strMsg += "\n - " + messpart + " ph\u1EA3i l\u1EDBn h\u01A1n " + (lower-1) + " k\u00FD t\u1EF1 v\u00E0 nh\u1ECF h\u01A1n " + (greater+1) + " k\u00FD t\u1EF1!";
        }
    }
    for (var i=0; i<ctrName.length; i++) {
        temp = "" + ctrName.substring(i, i+1);
        if (except==1){
            if (argSpecChar.indexOf(temp) != "-1"){
                strMsg += "\n - " + messpart +  " kh\u00F4ng \u0111\u01B0\u1EE3c nh\u1EADp c\u00E1c k\u00FD t\u1EF1: \n " + argSpecChar;
                break;
            }
        }else{
            if (argSpecChar.indexOf(temp) == "-1"){
                strMsg += "\n - " + messpart +  " ch\u1EC9 \u0111\u01B0\u1EE3c nh\u1EADp c\u00E1c k\u00FD t\u1EF1: \n" + argSpecChar;
                break;
            }
        }
    }
    return strMsg;
}
function CompDateThan(strDate1,strDate2){
    var m1, d1, y1;
    var m2, d2, y2;
    var s, pos1, pos2;
    var s = strDate1, s1 = strDate2;
    if (s.length == 0 || s1.length == 0) return true;

    pos1    = s.indexOf("/", 0);
    pos2    = s.indexOf("/", pos1+1);
    d1      = parseInt(s.substr(0, pos1), 10);
    m1      = parseInt(s.substr(pos1+1, pos2-pos1-1), 10);
    y1      = parseInt(s.substr(pos2+1, s.length-pos2), 10);

    pos1    = s1.indexOf("/", 0);
    pos2    = s1.indexOf("/", pos1+1);
    d2      = parseInt(s1.substr(0, pos1), 10);
    m2      = parseInt(s1.substr(pos1+1, pos2-pos1-1), 10);
    y2      = parseInt(s1.substr(pos2+1, s1.length-pos2), 10);

    var d1  = new Date(y1, m1, d1);
    var d2  = new Date(y2, m2, d2);
    if (d1 > d2) 
        return true;
    else
        return false;
}